package misp_backend.storage;


import io.minio.*;
import io.minio.http.Method;

import java.io.InputStream;
import java.util.concurrent.TimeUnit;

public class MinioStorage implements storage{
    private final MinioClient minioClient;
    private static final long PART_SIZE = 5 * 1024 * 1024;
    public MinioStorage(MinioClient minioClient) {
        this.minioClient = minioClient;
    }
    @Override
    public void putObject(String bucketName, String objectName, InputStream inputStream, String contentType) throws Exception {
        PutObjectArgs objectArgs = PutObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .stream(inputStream, -1, PART_SIZE)
                .contentType(contentType)
                .build();

        minioClient.putObject(objectArgs);
    }

    @Override
    public InputStream getObject(String bucketName, String objectName) throws Exception {
        GetObjectArgs getObjectArgs = GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        return minioClient.getObject(getObjectArgs);
    }

    @Override
    public String getObjectUrl(String bucketName, String objectName) throws Exception {
        GetPresignedObjectUrlArgs objectUrlArgs = GetPresignedObjectUrlArgs.builder()
                .bucket(bucketName)
                .object(objectName).
                method(Method.GET)
                .expiry(7, TimeUnit.DAYS).build();
        return minioClient.getPresignedObjectUrl(objectUrlArgs);
    }

    @Override
    public void removeObject(String bucketName, String objectName) throws Exception {
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        minioClient.removeObject(removeObjectArgs);
    }
}
